//
//  SecondTableViewCell.m
//  UI_19_数据请求model嵌套
//
//  Created by li cheng on 16/8/10.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "SecondTableViewCell.h"
#import "SecondCollectionViewCell.h"
#import "ExtensionList.h"
#import "UIImageView+DownloadImage.h"
@interface SecondTableViewCell()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *extensionListArr;
@end
@implementation SecondTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        self.collectionView = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
        CGFloat width = self.contentView.bounds.size.width;
        CGFloat height = self.contentView.bounds.size.height;
        CGFloat edge = 5;
        layout.itemSize = CGSizeMake( (width - edge * 4) / 3 , height - edge * 2);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
        self.collectionView.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_collectionView];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        [self.collectionView registerClass:[SecondCollectionViewCell class] forCellWithReuseIdentifier:@"SecondCollectionViewCell"];
        [self hotCarHandleDate];
    }
    return self;
}
- (void)hotCarHandleDate{
    NSString *hotCarPath = [[NSBundle mainBundle] pathForResource:@"Extension" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:hotCarPath];
    NSError *error = nil;
    NSDictionary *extensionDictionary = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
    NSArray *listArray = extensionDictionary[@"result"][@"list"];
    self.extensionListArr = [NSMutableArray array];
    for (NSDictionary *dic in listArray) {
        ExtensionList *extensionList = [[ExtensionList alloc] initWithDictionary:dic];
        [_extensionListArr addObject:extensionList];
    }
}
- (void)layoutSubviews{
    [super layoutSubviews];
    _collectionView.frame = self.contentView.bounds;
    NSLog(@"---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------%@", NSStringFromCGRect(self.contentView.bounds) );
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _extensionListArr.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SecondCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SecondCollectionViewCell" forIndexPath:indexPath];
    [cell.myImageView asyncDownloadImageWithURL:[_extensionListArr[indexPath.row] img] completionHandler:^(NSData *data, UIImage *image) {
        
    }];
    ExtensionList *extensionList = _extensionListArr[indexPath.row];
    cell.myLabel.text = extensionList.seriesname;
    cell.backgroundColor = [UIColor redColor];
    return cell;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
